package com.magicode.portal.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.AudienceRating;
import com.magicode.cms.entiy.base.Channel;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.Festival;
import com.magicode.cms.entiy.base.FilmBoxOffice;
import com.magicode.cms.entiy.base.MIndex;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.OfflineEvents;
import com.magicode.cms.entiy.base.Program;
import com.magicode.cms.entiy.base.Source;
import com.magicode.cms.entiy.base.Tag;
import com.magicode.cms.entiy.base.UserSubscription;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.entiy.base.VideoProgram;
import com.magicode.cms.service.impl.BaseServiceImpl;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.dao.PageResults;
import com.magicode.common.util.DateUtil;
import com.magicode.portal.service.IMainService;
@Service("MainService")
public class MainServiceImpl implements IMainService,UtilPath{

//	缓存间隔统一设置为5分钟
	private Integer cacheInterval = 20;
//	缓存时间key
	String cacheDateKey="date";
//	缓存数据key
	String cacheValueKey="value";
	String ignoreChannelCode = "'quicklytReport'"; 
	/**
	缓存对象，格式：
	key：输入参数；如果为空，设置为单字符串"";如果多个输入参数，将输入参数转化为字符串，使用,连接，例如：1,0,12；2,0,12
	value:Map 
			key:date
			value:缓存时间
			key:value
			value:缓存对象
	*/
//	视频节目关系缓存。用于显示视频首页检索条件
	Map<Object,Map<String,Object>> videoNameByAbcCache = null;
//  20150109zy仿写自视频节目关系缓存。实现影视黄页企业LOGO	
	Map<Object,Map<String,Object>> enterpriseLogoByAbcCache = null;
	//榜单收视率缓存
	Map<Object,Map<String,Object>> audienceRatingByAbcCache = null;
	@Override
	public PageResults Indexlist(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		//t.type !=3 排线下活动  t.head !=1 非头条文章， t.displayInTopPage = 1 首页显示
		String hql = " from "+Article.class.getName()+" t left outer join fetch t.mainPic "
				+ " where  t.status = 2 and t.head !=1 "
				+ "  and t.channel.code != 'quicklytReport' "
				+ " order by t.orderCode desc , t.subcribeTime desc " ;
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}
	
	@Override
	public PageResults Articlelist(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		String hql = " from "+Article.class.getName()+" t "
				+ " left outer join fetch t.mainPic "
				+ "  left outer join fetch t.source   " ;
//		排除速报
		String hql_where =" where t.status = 2 ";
//		按照频道设置排序
		String hql_order =" order by t.channelOrderTypeInt asc ,  t.subcribeTime desc ";
		if(null!=prams && !prams.isEmpty()){
			//某标签 id 的文章
			if(prams.containsKey("tagid")){
				String ids = this.getNewIdsByTagId(prams.get("tagid"));
				if(!"".equals(ids)){
					hql_where+=" and t.id in "+ids;
				}
				else{
					hql_where +=" and t.id is null";
				}
			}
			if(prams.containsKey("channelid")){
				String channelid = prams.get("channelid");
				//报告栏目特殊处理
				if("report".equals(channelid)){
					try {
						List<Object> chaid = commonDaoService.queryList("select t.id from "+Channel.class.getName()+" t where t.code='report' ", null);
						channelid = chaid.size()>0?chaid.get(0).toString():"";
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if(!"".equals(channelid)){
					hql+=" left outer join fetch t.channel  ";
					hql_where+=" and t.channel.id='"+channelid+"' ";
				}
			}
			//如果传入type
			if(prams.containsKey("type")){
				String typeName = prams.get("type");
				if(typeName != null){
						hql_where+=" and ( TO_DAYS(current_date()) - TO_DAYS( t.subcribeTime) <= 30 )";
						hql_order = " order by t.clickCount desc ,  t.subcribeTime desc ";
				}
			}
			
		}
		else{
			if(ignoreChannelCode != null){
				hql_where += " and t.channel.code not in ( "+ignoreChannelCode+" )";
			}
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}
	
	@Override
	public PageResults getSubscriptionNewsByUser(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		
		String hql = " select distinct n from "+UserSubscription.class.getName()+" t , "+News.class.getName()+" n  left outer join fetch n.mainPic  left outer join n.source ";
		String hql_where =" where t.tag in elements(n.tags) ";
//		按照频道设置排序
		String hql_order =" order by n.createTime desc, n.clickCount desc ";
		if(null!=prams && !prams.isEmpty()){
			//某标签 id 的文章
			if(prams.containsKey("userid")){
					hql_where+=" and t.user.id='"+prams.get("userid")+"' ";
			}
			
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		if(null != pageResults && pageResults.getResults().size() > 0){
		}
		List<Map<String, Object>> resultList = null;
		return pageResults;
	}

	/**
	 * 频道新闻列表
	 */
	@Override
	public PageResults channelArticlelist(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		String cId = prams.get("channelId");
		String hql = " from "+Article.class.getName()+" t "
				+ " left outer join fetch t.mainPic "				
				+ " left outer join fetch t.source  " ;
//		排除速报
		String hql_where =" where t.status = 2 ";
//		按照频道设置排序
		String hql_order =" order t.subcribeTime desc ";
		Map map = new HashMap();
		if(cId != null && cId.length() > 0){
			hql_where += " and t.channel.id = :cId ";
			map.put("cId", cId);
		}
		
		if(ignoreChannelCode != null){
			hql_where += " and t.channel.code not in ( "+ignoreChannelCode+" )";
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHqlMap(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, map);
		return pageResults;
	}
	@Override
	public PageResults Hotslist(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		String hql = " from "+News.class.getName()+" t left outer join fetch t.mainPic where  t.displayInTopPage = 1 and  t.status = 2 and  t.type !=3  " ;

		if(ignoreChannelCode != null){
			hql += " and t.channel.code not in ( "+ignoreChannelCode+" )";
		}
		if(null!=prams&&!prams.isEmpty()){
			//热门与时间排序查询条件
			if(prams.containsKey("searchtype")){
				String type = prams.get("searchtype");
				hql +=" and t.channel.id='"+prams.get("searchtype")+"'";
			}
			if(prams.containsKey("searchtype_time")){
				 Integer i = Integer.valueOf(prams.get("searchtype_time"));
				 String datastr="";
				 try {
					 switch (i) {
					 case 24:
					 datastr = DateUtil.getInstance().countDate(-1);
					 break;
					 case 7:
					datastr = DateUtil.getInstance().countDate(-7);
					 break;
					 case 30:
					 datastr = DateUtil.getInstance().countDate(-30);
					break;
					default:
					 break;
				}
				 hql+=" and t.createTime > '"+datastr+"'";
				 
				 } catch (Exception e) {
				 // TODO: handle exception
				}
				
			}
			hql += " order by t.subcribeTime desc ";
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}

	/**
	 * 模式库分页
	 */
	@Override
	public PageResults Videolist(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		String hql = "select distinct t from "+Video.class.getName()+" t "
				+ " left outer join fetch t.mainPic "
				+ " left outer join fetch t.area  " ;
		
		String hql_where =" where t.channel.code = 'videoProgram' and "
				+ "  t.status = 2  and t.freeVideoList is not null  and t.displayInTopPage = 1  ";
		String hql_order =" order by  t.subcribeTime desc, t.playCount desc ";
		Map paraMap = new HashMap();
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("sort")){
				if("timesort".equals(prams.get("sort"))){
					 hql_order =" order by  t.subcribeTime desc, t.playCount desc ";
				}
				else if("timedsort".equals(prams.get("sort"))){
					 hql_order =" order by t.subcribeTime asc , t.playCount asc  ";
				}
				else if("playtimes".equals(prams.get("sort"))){
					 hql_order =" order by t.playCount desc ,t.subcribeTime desc ";
				}
				else {
					 
				}
			}
//			查询标题
			if(prams.containsKey("title")){
				hql_where+=" and ( t.translateTitle like :title or t.title like :title ) ";
				paraMap.put("title", "%"+prams.get("title")+"%");
			}
//			如果有出现other查询地区type为other的地区
			if(prams.containsKey("sel_video_area")){
				String areaId = prams.get("sel_video_area");
				if(!"other".equals(areaId)){
					hql_where+=" and ( t.area.id = :sel_video_area or t.area.parent.id = :sel_video_area ) ";
					paraMap.put("sel_video_area", prams.get("sel_video_area"));
				}
				else{
					hql_where+=" and ( t.area.type = :sel_video_area or t.area.parent.type = :sel_video_area ) ";
					paraMap.put("sel_video_area", "other");
				}
			}
			if(prams.containsKey("channelid")){
				hql+=" left outer join fetch t.channel  ";
				hql_where+=" and t.channel.id = :channelid ";
				paraMap.put("channelid", prams.get("channelid"));
			}
			//字母
			if(prams.containsKey("videoProgramA")){
				hql_where+=" and t.videoProgram.initialWordDic.code=:videoProgramA ";
				paraMap.put("videoProgramA", prams.get("videoProgramA").toUpperCase());
			}
			//节目id
			if(prams.containsKey("videoProgramid")){
				hql_where+=" and t.videoProgram.id= :videoProgramid ";
				paraMap.put("videoProgramid", prams.get("videoProgramid"));
			}
			//某标签 id 的文章
			if(prams.containsKey("tagid")){
				String ids = this.getNewIdsByTagId(prams.get("tagid"));
				if(!"".equals(ids)){
					hql_where+=" and t.id in "+ids;
				}
				else{
					hql_where +=" and t.id is null";
				}
			}
			if(prams.containsKey("sel_video_type")){
				hql+=" , "+Dictionary.class.getName()+" type  ";
				hql_where+=" and type in elements(t.videoTypeList)  "
						+ " and (type.parent.id = :sel_video_type or type.id = :sel_video_type ) ";
				paraMap.put("sel_video_type", prams.get("sel_video_type"));
			}
		}
		//30天以内
		try {
			//hql_where+=" and t.createTime > '"+DateUtil.getInstance().countDate(-30)+"'";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHqlMap(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, paraMap);
		
		return pageResults;
	}
	
	/**
	 * V视分页
	 */
	@Override
	public PageResults vVideoList(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		String hql = "select t from "+Video.class.getName()+" t "
				+ " left outer join fetch t.mainPic "
				+ " left outer join fetch t.area  " ;
		String hql_where =" where   t.channel.code != 'videoProgram' and  t.status = 2 and t.freeVideoList is not null and t.displayInTopPage = 1  ";
		String hql_order =" order by  t.subcribeTime desc, t.playCount desc ";
		Map paraMap = new HashMap();
		if(null!=prams && !prams.isEmpty()){
			
			if(prams.containsKey("sel_video_type")){
				hql+=" , "+Dictionary.class.getName()+" type  ";
				hql_where+=" type in elements(t.videoTypeList)  "
						+ " and (type.parent.id = :sel_video_type or type.id = :sel_video_type ) ";
				paraMap.put("sel_video_type", prams.get("sel_video_type"));
			}
		}
		//30天以内
		try {
			//hql_where+=" and t.createTime > '"+DateUtil.getInstance().countDate(-30)+"'";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHqlMap(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, paraMap);
		return pageResults;
	}
	@Override
	public PageResults Officelinelist(Map<String,String> prams, Integer pageNo,
			Integer pageSize) {
		String hql = " from "+OfflineEvents.class.getName()+" t left outer join fetch t.mainPic  " ;
		String hql_where =" where  t.status = 2   ";
		String hql_order =" order by t.beginTime desc ";
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("channelid")){
				hql+=" left outer join fetch t.channel  ";
				hql_where+=" and t.channel.id='"+prams.get("channelid")+"' ";
			}
			if(prams.containsKey("office_type")){
				hql+=" left outer join fetch t.eventSubject  ";
				hql_where+=" and t.eventSubject.id='"+prams.get("office_type")+"' ";
			}
			if(prams.containsKey("office_city")){
				hql+=" left outer join fetch t.city  ";
				hql_where+=" and t.city.id='"+prams.get("office_city")+"' ";
			}
			if(prams.containsKey("status")){
//				hql+="   ";
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//				如果是正在进行
				if("true".equals(prams.get("status"))){
					hql_where += " and "+sql_date_method+"(t.endTime,'"+sql_date_format+"') >= '"+sdf.format(date)+"'";
				}
//				已经结束
				else{
					hql_where += " and "+sql_date_method+"(t.endTime,'"+sql_date_format+"') < '"+sdf.format(date)+"'";
				}
			}
			//某标签 id 的文章
			if(prams.containsKey("tagid")){
				String ids = this.getNewIdsByTagId(prams.get("tagid"));
				if(!"".equals(ids)){
					hql_where+=" and t.id in "+ids;
				}
				else{
					hql_where +=" and t.id is null";
				}
			}
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}

	@Override
	public PageResults channellist(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		String hql = " from "+ Channel.class.getName()+" t left outer join fetch t.mainPic  " ;
		String where =" where 1=1 ";
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("id")){
				where+= " and t.id='"+prams.get("id")+"'";
			}
			if(prams.containsKey("code")){
				where+= " and t.code='"+prams.get("code")+"'";
			}
			if(prams.containsKey("parentid")){
				hql=" left outer join fetch t.parent  ";
				where+= " and t.parent.id='"+prams.get("parentid")+"'";
			}
		}
		String orderhql=" order by t.createTime desc ";
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+where+orderhql, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}
	
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	public ICommonDaoService getCommonDaoService() {
		return commonDaoService;
	}

	public void setCommonDaoService(ICommonDaoService commonDaoService) {
		this.commonDaoService = commonDaoService;
	}
	private String getNewIdsByTagId(String tagid){

		String sqlString = " select n.id  from com.magicode.cms.entiy.base.Tag tag , com.magicode.cms.entiy.base.News n where "
				+ "  n.status = 2 and  tag in elements(n.tags) and tag.id ='"+tagid+"' " ;
		List<String>  news_ids = commonDaoService.getListByHQL(sqlString);
		
		if(null!=news_ids&&news_ids.size()>0){
			String ids="("; 
			for(String temp : news_ids){
				ids+="'"+temp+"'"+",";
			}
			ids = ids.substring(0, ids.length()-1);
			ids+=")";
			return ids;
		}
		return "";
	}

	/**
	 * 返回格式：
	 * List<Map<String,Object>>
	 * iwdId:
	 * idwTitle:
	 * enterpriseList:
	 * 				  entId
	 * 				  entTitle
	 * 				  mainPicId
	 * 				  mainPicMinPath
	 * 				
	 */
	@Override
	public List<Map<String, Object>> getEnterpriseLogoByAbc() {
		String iwdId = "iwdId";
		String idwTitle = "idwTitle";
		String enterpriseList = "enterpriseList";
		String entId = "entId";
		String entTitle = "entTitle";
		String mainPicId = "mainPicId";
		String mainPicMinPath = "mainPicMinPath";
//		从缓存获取
		String cacheKey = "";
		String result =  (String) getCacheData(enterpriseLogoByAbcCache,cacheKey);
		List<Map<String,Object>> resultList = new ArrayList();
		Map<String, Object> strObj = new HashMap();
//		如果为空，说明没有数据
		if(result == null){
			LinkedHashMap<String,Object> lmap = new LinkedHashMap<String,Object>();
			try {
//				只显示影视黄页中存在的首字母
//				String abchql = "select new Map( t.id as "+iwdId+" , t.title as "+idwTitle+" ,   " +
//						" e.id as "+entId+" , e.title  as "+entTitle+" , mp.id as "+mainPicId+" , mp.minPath as "+mainPicMinPath+" )" 
//						+" from "+Dictionary.class.getName()+" t, "
//						+Enterprise.class.getName()+" e left join e.mainPic as mp "
//						+" where e.initialWordDic = t and e.status = 2 order by t.orderCode asc , e.orderCode asc , e.head desc ) ";
//				未发布的企业也会被显示出来
//				String abchql = "select new Map( t.id as "+iwdId+" , t.title as "+idwTitle+" ,   " +
//						" e.id as "+entId+" , e.title  as "+entTitle+" , mp.id as "+mainPicId+" , mp.minPath as "+mainPicMinPath+" )" 
//						+" from "
//						+Enterprise.class.getName()+" e  "
//						+" left join e.mainPic as mp right join e.initialWordDic as t"
//						+" where  t.dicType.code='InitialWord' order by t.orderCode asc , e.orderCode asc , e.head desc ) ";
				
				String inithql = "from "+Dictionary.class.getName()+" t where t.dicType.code='InitialWord' order by t.orderCode asc ";
				String abcdhql = "from "+Enterprise.class.getName()+" e left outer join fetch e.mainPic as mp left outer join fetch e.initialWordDic where e.status = 2 order by e.orderCode asc , e.head desc ";
				List<Dictionary> inits = commonDaoService.queryList(inithql, null);
				List<Enterprise> enterList = commonDaoService.queryList(abcdhql, null);
				
				for (Dictionary init : inits) {
					Map iwdMap = new HashMap();
					iwdMap.put(iwdId, init.getId());
					iwdMap.put(idwTitle, init.getTitle());
					List<Map<String,Object>> entList = new ArrayList();
					for (Enterprise enterprise : enterList) {
						Map entMap = new HashMap();
						List<Map> eList = new ArrayList();
						if((enterprise.getInitialWordDic().getTitle()).equals(init.getTitle())){
							entMap.put(entId, enterprise.getId());
							entMap.put(entTitle, enterprise.getTitle());
							if(enterprise.getMainPic()!=null){
								entMap.put(mainPicId, enterprise.getMainPic().getId());
								entMap.put(mainPicMinPath, enterprise.getMainPic().getMinPath());
							}else{
								entMap.put(mainPicId, "");
								entMap.put(mainPicMinPath, "");
							}
							entList.add(entMap);
						}
						iwdMap.put(enterpriseList, entList);
					}
					resultList.add(iwdMap);
				}
			
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			Map tempMap = new HashMap();
			tempMap.put("key", resultList);
			JSONArray jsarray = new JSONArray(resultList);
			result= jsarray.toString();
//			将放入数据放入缓存
//			enterpriseLogoByAbcCache = addCacheData(enterpriseLogoByAbcCache,cacheKey,result);
		}
		return resultList;
	}
	
	@Override
	public String getVideoNameByAbc(){
		String cacheKey = "";
//		从缓存获取
		String result =  (String) getCacheData(videoNameByAbcCache,cacheKey);
//		如果为空，说明没有数据
		if(result == null){
			List<Map> resultList = new ArrayList();
			LinkedHashMap<String,Object> lmap = new LinkedHashMap<String,Object>();
			try {
				String inithql = " select t.code as code  from "+Dictionary.class.getName()+" t where t.dicType.code='InitialWordVP' order by t.orderCode asc ";
				List<String> inits = commonDaoService.queryList(inithql, null);
				for(String obj : inits){
//					String hql=" select  new Map( p.id as value ,p.title as text )  from "+VideoProgram.class.getName()+" p  where  p.initialWordDic.code='"+obj+"'" ;
					String hql=" select  new Map( p.videoProgram.id as value ,p.title||' '||p.translateTitle as text,p.id as id )  from "+Video.class.getName()+" p  where and p.freeVideoList is null and p.videoProgram.initialWordDic.code='"+obj+"'" ;
					List<Map<String,Object>> list = commonDaoService.queryList(hql, null);
					Map<String,Object> map = new HashMap<String,Object>();
					if(null!=list && list.size()>0){
						map.put(obj.toLowerCase(), list);
						lmap.put(obj.toLowerCase(), list);
					}else{
						map.put(obj.toLowerCase(), new ArrayList<Map<String,Object>>());
						lmap.put(obj.toLowerCase(), new ArrayList<Map<String,Object>>());
					}
					resultList.add(map);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			Map tempMap = new HashMap();
			tempMap.put("key", resultList);
			JSONArray jsarray = new JSONArray(resultList);
			result= jsarray.toString();
//			将放入数据放入缓存
			videoNameByAbcCache = addCacheData(videoNameByAbcCache,cacheKey,result);
		}
		return result;
	}
	
	
	/**
	 * 得到缓存数据
	 * @param cacheMap
	 * @param cacheParam
	 * @return
	 */
	protected Object getCacheData(Map cacheMap , Object cacheParam ){
		Object result = null;
//		当前时间
		Date currentDate = new Date();
//		从缓存读取匹配参数的Map
		Map map = null;
		if(cacheMap != null){
			map = (Map) cacheMap.get(cacheParam);
		}
//		读取对象缓存时间
		Date date =  null;
		if(map != null ){
			date = (Date)map.get(cacheDateKey);
		}
//		如果时间为空，说明没有设置
		if(date == null){
			return null;
		}
		else{
			long currentDateLong = currentDate.getTime();
			long cachDateLong = date.getTime();
//			如果超出间隔时间，清空缓存
			if((currentDateLong- cachDateLong )/1000/60 > cacheInterval ){
				date = currentDate;
				cacheMap.remove(cacheParam);
			}
//			如果在间隔内，从缓存数据读取
			else{
				result = map.get(cacheValueKey);
			}
		}
		return result;
	}
	/**
	 * 设置缓存数据
	 * @param cacheMap
	 * @param cacheParam
	 * @param data
	 * @return
	 */
	protected Map addCacheData(Map cacheMap , Object cacheParam , Object realData){
		Object result = null;
//		当前时间
		Date currentDate = new Date();
//		从缓存读取匹配参数的Map
		Map map = null;
		if(cacheMap != null){
			map = (Map) cacheMap.get(cacheParam);
		}
//		读取对象缓存时间
		Date date = null;
//		第一次初始化cacheMap
		if(cacheMap == null){
			cacheMap = new HashMap();
		}
//		初始化map
		if(map == null){
			map = new HashMap();
		}
		date = (Date)map.get(cacheDateKey);
		Object cacheData = (Object)map.get(cacheValueKey);
//		如果时间为空，说明没有设置，将数据放入缓存
		if(date == null || cacheData == null){
			map.put(cacheDateKey, currentDate);
			map.put(cacheValueKey, realData);
			cacheMap.put(cacheParam, map);
		}
		else{
			long currentDateLong = currentDate.getTime();
			long cachDateLong = date.getTime();
//			如果超出间隔时间，清空缓存
			if((currentDateLong- cachDateLong )/1000/60 > cacheInterval ){
				map.put(cacheDateKey, currentDate);
				map.put(cacheValueKey, realData);
				cacheMap.put(cacheParam, map);
			}
		}
		return cacheMap;
	}

	@Override
	public PageResults Enterpriselist(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		String hql = " from "+ Enterprise.class.getName()+" e left outer join fetch e.mainPic  " ;
		hql+=" left outer join fetch e.country  ";
		hql+=" left outer join fetch e.initialWordDic  ";
		hql+=" left outer join fetch e.types tp ";
		
//		if(null!=prams && !prams.isEmpty() && prams.containsKey("enter_cmpstype")){
//			hql = hql+" , "+Dictionary.class.getName()+" tp ";
//		}
		String where =" where 1=1 and e.status = 2 ";
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("id")){
				where+= " and e.id='"+prams.get("id")+"'";
			}
			//按名称
			if(prams.containsKey("enter_cmpsname")){
				where+=" and e.title like '%"+prams.get("enter_cmpsname")+"%' ";
			}
			//按类型
			if(prams.containsKey("enter_cmpstype")){
				where += " and tp.id = '"+prams.get("enter_cmpstype")+"' and tp in elements(e.types) ";
			}
			//按地区
			if(prams.containsKey("enter_cmpscity")){
				where+=" and e.country.id='"+prams.get("enter_cmpscity")+"' ";
			}
			//按首字母
			if(prams.containsKey("enter_cmpsword")){
				where+=" and e.initialWordDic.id='"+prams.get("enter_cmpsword")+"' ";
			}
		}
		String orderhql=" order by e.head desc , e.displayInTopPage desc ,e.orderCode asc, e.lastUpdateTime desc ";
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+where+orderhql, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}

	@Override
	public PageResults audienceRatinglist(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		String hql = " from "+AudienceRating.class.getName()+" t left outer join fetch t.type  " ;
		String hql_where =" where  1=1 ";
		String hql_order =" order by t.orderCode desc , t.subcribeTime desc ";
		
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("typeId")){
				hql_where+=" and t.type.id='"+prams.get("typeId")+"' ";
			}
			if(prams.containsKey("date")){
				hql_where+=" and t.statisticsDate='"+prams.get("date")+"' ";
			}
		}else{
			hql_where+=" and t.type.id= '402883084ca69f05014ca6a76b4d0001' ";
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}

	@Override
	public PageResults mindexlist(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		String hql = " from "+MIndex.class.getName()+" t left outer join fetch t.tpWeek " ;
		String hql_where =" where  1 = 1   ";
		String hql_order =" order by t.orderCode desc , t.subcribeTime desc ";
		if(null!=prams && !prams.isEmpty()){
			/*if(prams.containsKey("date")){
				hql_where+=" and t.statisticsDate='"+prams.get("date")+"' ";
			}*/
			if(prams.containsKey("weekId")){
				hql_where+=" and t.tpWeek.id='"+prams.get("weekId")+"' ";
			}
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}

	@Override
	public PageResults filmlist(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		String hql = " from "+FilmBoxOffice.class.getName()+" t " ;
		String hql_where =" where  1 = 1   ";
		String hql_order =" order by t.orderCode desc , t.subcribeTime desc ";
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("date")){
				hql_where+=" and t.statisticsDate='"+prams.get("date")+"' ";
			}
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}

	@Override
	public PageResults featrueslist(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		String hql = " from "+Article.class.getName()+" t left outer join fetch t.channel" ;
				hql+=" left outer join fetch t.mainPic ";
				hql+=" left outer join fetch t.relaPics ";
		String hql_where =" where  t.channel.id = '8a2c5c5249713056014973a09a6e0004' ";
		String hql_order =" order by t.orderCode desc , t.subcribeTime desc ";
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("billBoardType")){
				hql=" left outer join fetch t.billBoardType  ";
				hql_where+=" and t.billBoardType.id='"+prams.get("billBoardType")+"' ";
			}
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}

	@Override
	public PageResults festivallist(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		String hql = " from "+ Festival.class.getName()+" f ";
		hql+=" left outer join fetch f.mainPic ";
		hql+=" left outer join fetch f.festivalDic ";
		hql+=" left outer join fetch f.year ";
		hql+=" left outer join fetch f.area ";
		String where =" where 1=1 and f.status = 2 and f.valid = 1 ";
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("id")){
				where+= " and e.id='"+prams.get("id")+"'";
			}
			//按地区搜索
			if(prams.containsKey("fest_area")){
				where+=" and f.area.id='"+prams.get("fest_area")+"' ";
			}
			//按关键字（名称）搜索
			if(prams.containsKey("fest_name")){
				where+=" and f.festivalDic.titleCn like '%"+prams.get("fest_name")+"%' ";
			}
			//按时间搜索
			if(prams.containsKey("fest_beginTime")){
				where+=" and f.endTime>'"+prams.get("fest_beginTime")+"' ";
			}
			if(prams.containsKey("fest_endTime")){
				where+=" and e.beginTime<'"+prams.get("fest_endTime")+"' ";
			}
		}
		String orderhql=" order by f.head desc , f.orderCode asc , f.subcribeTime desc ";
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+where+orderhql, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}
	
	@Override
	public PageResults VideolistVedu(Map<String,String> prams, Integer pageNo, Integer pageSize) {
		String hql = " from "+Video.class.getName()+" t left outer join fetch t.mainPic " ;
		String hql_where =" where  t.status = 2 and   t.freeVideoList is not null  "
				+ " and (t.channel.id='8a2c5c5249781b22014978fa0f180005' or t.channel.id='8a2c5c5249ce14ae0149d11ad4700020' ) ";
			//	+ "	and t.channel.id='8a2c5c5249781b22014978f937c90004' ";
		String hql_order =" order by  t.subcribeTime desc, t.playCount desc ";
		if(null!=prams && !prams.isEmpty()){
			if(prams.containsKey("sortList")){
				if("HotPlaySort".equals(prams.get("sortList"))){
					 hql_order =" order by t.playCount desc ,t.subcribeTime desc ";
				}
				else if("HotCommentSort".equals(prams.get("sortList"))){
					 hql_order =" order by t.commentsCount asc ,t.subcribeTime desc ";
				}
				else {
					 
				}
			}
			//某标签 id 的文章
			if(prams.containsKey("tagid")){
				String ids = this.getNewIdsByTagId(prams.get("tagid"));
				if(!"".equals(ids)){
					hql_where+=" and t.id in "+ids;
				}
				else{
					hql_where +=" and t.id is null";
				}
			}
		}
		//30天以内
		try {
			//hql_where+=" and t.createTime > '"+DateUtil.getInstance().countDate(-30)+"'";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql+hql_where+hql_order, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
	}
	@Override
	public PageResults setNewsTagsByPr( PageResults pageResults ){

		String tagHql = "select distinct new map(t.id as id , d as title ) from "+Tag.class.getName()+" d , "+News.class.getName()+" t "
				+ " where  d in elements(t.tags) " ;
		String propertyName = "tags";
		String connectStr = null;
		try {
			pageResults = (PageResults) commonDaoService.getRelateProByList(tagHql, connectStr, propertyName, pageResults);
		}catch(Exception e){
			e.printStackTrace();
		}
		return pageResults;
		
	}
	
	@Override
	public PageResults setNewsSourceByPr( PageResults pageResults ){

		String tagHql = "select distinct new map(t.id as id , d as title ) from "+Source.class.getName()+" d , "+News.class.getName()+" t "
				+ " where  d=t.source " ;
		String propertyName = "source";
		String connectStr = null;
		try {
			pageResults = (PageResults) commonDaoService.getRelateProByList(tagHql, connectStr, propertyName, pageResults);
		}catch(Exception e){
			e.printStackTrace();
		}
		return pageResults;
		
	}
	@Override
	public List setNewsTagsByList( List List ){

		String tagHql = "select distinct new map(t.id as id , d as title ) from "+Tag.class.getName()+" d , "+News.class.getName()+" t "
				+ " where  d in elements(t.tags) " ;
		String propertyName = "tags";
		String connectStr = null;
		try {
			List = (List) commonDaoService.getRelateProByList(tagHql, connectStr, propertyName, List);
		}catch(Exception e){
			e.printStackTrace();
		}
		return List;
		
	}

	@Override
	public List<Dictionary> getVideoNameTypeByTypeDic(){
		String cacheKey = "";
		List<Dictionary> result = null;
		List<Map> resultList = new ArrayList();
		LinkedHashMap<String,Object> lmap = new LinkedHashMap<String,Object>();
		try {
			String inithql = " select  t "
							+ "  from "+Dictionary.class.getName()+" t    "
							+ " where t.valid = 1 and t.dicType.code='VideoType' "
							+ " and ( t.type=null or t.type='') and t.parent=null "
							+ " order by t.orderCode asc ";
			result = commonDaoService.queryList(inithql, null);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public PageResults programList(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults personList(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults IndexNewsToplist(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getVideoNameTypeByType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults IndexNewsCommentlist(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults IndexHitForMatlist(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults ArticleTpiclist(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List audienceRatinglistDate(Map<String, String> prams) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults Newslist(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageResults ArticleReportlist(Map<String, String> prams,
			Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List audienceRatinglistDateDis(Map<String, String> prams) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
